package com.fourhats.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fourhats.services.BlogService;
import com.google.gson.Gson;

@Controller
public class BlogController extends GenericController {

	@Autowired
	private BlogService blogService;
	
	@RequestMapping("/blog")
    public String goToBlog(Model model) {
		Gson gson = new Gson();
		model.addAttribute("posts", gson.toJson(this.blogService.getAllOrderByDate()).toString());
		return "content/blog/blog";
	}
	
	@RequestMapping("/post/{postId}/{postTitle}")
    public String goToPost(@PathVariable long postId, @PathVariable String postTitle, Model model) {
		Gson gson = new Gson();
		model.addAttribute("post", gson.toJson(this.blogService.getById(postId)).toString());
        return "content/blog/post";
	}
}
